Thread: Help with LISTS [Urgent]

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    105

    Help with LISTS [Urgent]

    Hello everyone. I've been all the day with a problem and I can't fix it. I hope you can help me.

    I have a company which has different areas (for example: Marketing, Sales, etc.) and each area has different employees. Each employee has a name and an age (for example: John, 27)

    This are my structs (I just can use this two)

    Code:
    typedef struct employee{
        char name[20];
        int age;
        struct employee* next;
        struct employee* nextAge;
    }tEmployee;
    
    typedef struct company{
        char area[10];
        tEmployee* first; // points to the first of each area
    }tCompany;
    I don't know how to do the first part of this problem (I suppose I can do the rest on my own). I have to ask for the information and put it in the program.

    This is what I think I have to do:
    - First, I ask how many areas has the company.
    - I ask for memory of that amount of areas (I know I have to use a realloc, I use it a lot of times, but I don't know what to put in NULL so I can use realloc)
    - Then, I ask which area has an employee. For example: Area 1 - 2 employees: John (27) and Louis (29)
    - I look in the list for the last position so I put there the new node, like this:
    Code:
    while(company->first!=NULL)
        company->first=company->first->sig;
    and then I put there the information.


    If I only had 1 area, I could do:
    Code:
    void fillDataOneEmployee(tCompany **company)
    {
        while(((*company)->first->next)!=NULL)
            (*company)->first=(*company)->first->next;
        printf("\nName: ");
        scanf("%s", ((*company)->first->name));
        printf("\nAge: ");
        scanf("%s", ((*company)->first->age));
        (*company)->first->next=NULL;
    }
    (this code is for 1 employee)


    Then I have to do a lot of things with that but I think I can do that on my own.




    Can you help me how to start the program, please?
    Thanks!!!!!
    Last edited by juanjuanjuan; 12-13-2014 at 03:50 PM.

  2. #2
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    This is what I'm trying to do:
    Help with LISTS [Urgent]-img_20141213_180442034-jpg

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Why is there a next pointer and a next age pointer? Is this a list or a tree structure?

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Because this is just the beginning of the exercise. Then I have to order them by age and a lot of things more, but I suppose I can do that on my own.
    That's why I'm asking you for the first part of the exercise

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    I forgot to mention that in main I have this:

    Code:
    int main()
    {
        tCompany vector[5];
        return 1;
    }

  6. #6
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Any help, please?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    First create a function to allocate and initialise an employee struct

    Code:
    tEmployee *newEmployee(char name, int age) {
      tEmployee *n = malloc(sizeof(*n));
      if ( n ) {
        // initialise every member
        strcpy(n->name,name);
        n->age = age;
        n->next = NULL;
      }
      return n;
    }
    Next, add to a list
    Code:
    tEmployee *add(tEmployee *head, tEmployee *node ) {
      node->next = head;
      return node;
    }
    Basically, you're trying to do too much in a single function with all those ->...->... lines.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-23-2013, 06:11 AM
  2. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  3. Urgent help please
    By watsee in forum C Programming
    Replies: 14
    Last Post: 02-09-2010, 12:44 PM
  4. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  5. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM